//
//  QYCertificationViewController.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/8.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "QYCertificationViewController.h"

@interface QYCertificationViewController ()
@property (nonatomic, strong) UIImageView *zmImageView;// 正面照
@property (nonatomic, strong) UIImageView *fmImageView;// 反面照
@property (nonatomic, strong) UIImage *zmImage;// 正面照
@property (nonatomic, strong) UIImage *fmImage;// 反面照
@end

@implementation QYCertificationViewController
- (void)setDataDic:(NSDictionary *)dataDic
{
    // 营业执照资源服务器地址
    self.zmImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:dataDic[@"companyidcard_front"]]]];
    // 公司照片资源服务器地址
    self.fmImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:dataDic[@"companyidcard_back"]]]];
    NSLog(@"aaa");
}
- (void)saveJobToServer{
    [self showprogressHUD];
    NSDictionary *parameter = @{@"user_id":[UserModel sharedInstanced].userId,@"token":[UserModel sharedInstanced].token};
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=saveinfo" parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if (!self.zmImage) {
            self.zmImage = [UIImage imageNamed:@"companey_back"];
        }
        NSData *data = UIImageJPEGRepresentation(self.zmImage, .1);
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        [formData appendPartWithFileData:data name:@"licence" fileName:[NSString stringWithFormat:@"%ld.jpg",(NSUInteger)time] mimeType:@"image/jpeg"];
        
        if (!self.fmImage) {
            self.fmImage = [UIImage imageNamed:@"companey_front"];
        }
        NSData *data2 = UIImageJPEGRepresentation(self.fmImage, .1);
        NSTimeInterval time2 = [[NSDate date] timeIntervalSince1970];
        [formData appendPartWithFileData:data2 name:@"company_img" fileName:[NSString stringWithFormat:@"%ld.jpg",(NSUInteger)time2] mimeType:@"image/jpeg"];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
            [self.view makeToast:@"上传成功" duration:1 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
        [self hiddenProgressHUD];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
    }];
}

- (void) btnAction:(UIButton *)btn
{
    [self saveJobToServer];
}
- (void)imagesViewTap:(UITapGestureRecognizer *)tapgesture
{
    __weak typeof(self)weakself = self;
    if ([tapgesture.view isEqual:self.zmImageView]) {
        NSLog(@"正面");
        [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) {
            NSLog(@"上传头像");
            weakself.zmImageView.image = img;
            weakself.zmImage = img;
        }];
    }else if ([tapgesture.view isEqual:self.fmImageView])
    {
        NSLog(@"反面");
        [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) {
            NSLog(@"上传头像");
            weakself.fmImageView.image = img;
            weakself.fmImage = img;
        }];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"上传证件材料";
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 / WIDTH_6S_SCALE, 20 / HEIGHT_6S_SCALE, SCREEN_WIDTH, 20 / HEIGHT_6S_SCALE)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = getColor(blackColor);
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = DEF_FontSize_16;
    titleLabel.text = @"上传证件材料";
    [self.view addSubview:titleLabel];
    [self.view addSubview:self.zmImageView];
    [self.view addSubview:self.fmImageView];
    
    __weak typeof(self)weakself = self;
    [self.zmImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(20 / HEIGHT_6S_SCALE);
        make.left.equalTo(titleLabel.mas_left);
        make.right.equalTo(weakself.view.mas_right).offset(-20 / HEIGHT_6S_SCALE);
        make.height.mas_equalTo(149 / HEIGHT_6S_SCALE);
    }];
    [self.fmImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.zmImageView.mas_bottom).offset(20 / HEIGHT_6S_SCALE);
        make.left.equalTo(titleLabel.mas_left);
        make.right.equalTo(weakself.view.mas_right).offset(-20 / HEIGHT_6S_SCALE);
        make.height.mas_equalTo(149 / HEIGHT_6S_SCALE);
    }];
    
    UIButton * upPostBtn = [[UIButton alloc]init];
    upPostBtn.backgroundColor = getColor(mainColor);
    [upPostBtn setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
    [upPostBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [upPostBtn setTitle:@"上传" forState:UIControlStateNormal];
    upPostBtn.layer.cornerRadius = 5;
    upPostBtn.layer.masksToBounds = YES;
    [self.view addSubview:upPostBtn];
    
    [upPostBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.fmImageView.mas_bottom).offset(40 / HEIGHT_6S_SCALE);
        make.left.equalTo(weakself.view.mas_left).offset(20 / WIDTH_6S_SCALE);
        make.right.equalTo(weakself.view.mas_right).offset(-20 / WIDTH_6S_SCALE);
        make.height.mas_equalTo(50 / HEIGHT_6S_SCALE);
    }];
    if (self.zmImage) {
        self.zmImageView.image = self.zmImage;
    }
    if (self.fmImage) {
        self.fmImageView.image = self.fmImage;
    }
}
- (UIImageView *)zmImageView
{
    if (!_zmImageView) {
        _zmImageView = [[UIImageView alloc]init];
        _zmImageView.backgroundColor = [UIColor clearColor];
        _zmImageView.userInteractionEnabled = YES;
        _zmImageView.image = [UIImage imageNamed:@"companey_back"];
        UITapGestureRecognizer * imagesViewTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imagesViewTap:)];
        [_zmImageView addGestureRecognizer:imagesViewTapGestureRecognizer];
    }
    return _zmImageView;
}
- (UIImageView *)fmImageView
{
    if (!_fmImageView) {
        _fmImageView = [[UIImageView alloc]init];
        _fmImageView.backgroundColor = [UIColor clearColor];
        _fmImageView.userInteractionEnabled = YES;
        _fmImageView.image = [UIImage imageNamed:@"companey_front"];
        UITapGestureRecognizer * imagesViewTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imagesViewTap:)];
        [_fmImageView addGestureRecognizer:imagesViewTapGestureRecognizer];
    }
    return _fmImageView;
}

@end

